package com.hsm.job;

import com.hsm.constant.RedisConstant;
import com.hsm.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 定时清理垃圾图片任务类
 */
@Component
public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 清理垃圾图片业务方法实现
     */
    public void clearImg(){
        System.out.println("开始定时任务了************************");
        //1集合A-集合B 得到差集
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);

        System.out.println("处理定时任务****************size********"+sdiff.size());
        //2循环遍历差集
        if(!CollectionUtils.isEmpty(sdiff)){
            for (String imgName : sdiff) {
                //2.1调用七牛云工具类删除图片
                QiniuUtils.deleteFileFromQiniu(imgName);
                //2.2将redis中集合A的垃圾图片删除
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,imgName);
            }
        }
        System.out.println("结束定时任务了************************");
    }
}
